#!/bin/env ruby
# encoding: utf-8

def validate_orgName (orgName)
  text_field(class: "b-form__input_text", name: "companyName", id: "companyName").clear
  text_field(class: "b-form__input_text", name: "companyName", id: "companyName").set orgName
  button(text: "Submit").click 
  browser.wait(3)        
  (span(class: "b-form__error", id: "companyName.errors").text ==  
    'Can contain only letters, digits and "-","_",",", " " symbols with a leading letter')
    .should be_truthy    
end

def validate_orgName_true (orgName)
  firstName = text_field(class: "b-form__input_text", name: "firstName", id: "firstName").text
  text_field(class: "b-form__input_text", name: "firstName", id: "firstName").clear
  text_field(class: "b-form__input_text", name: "companyName", id: "companyName").clear
  text_field(class: "b-form__input_text", name: "companyName", id: "companyName").set orgName
  button(text: "Submit").click       
  browser.wait(3)
  (span(class: "b-form__error", id: "companyName.errors").exist?).should be_falsey  
  text_field(class: "b-form__input_text", name: "firstName", id: "firstName").set firstName
end

#**************************************************************************************************#
shared_context "User names" do
   
  context "when submit First Name" do
    it "then it can not be empty"  do      
      text_field(name: "firstName", id: "firstName").clear
      button(text: "Submit").click      
      span(class: "b-form__error", id: "firstName.errors", text: "Please enter info here.").wait_until_present(5)
    end
    it "then it can contain les than or equal to 50 characters"  do      
      text_field(name: "firstName", id: "firstName").clear
      text_field(name: "firstName", id: "firstName").set str(55)
      button(text: "Submit").click       
      span(class: "b-form__error", id: "firstName.errors", text: "Can contain less than or equal to 50 characters").wait_until_present(5)
    end
  end
  
  context "when submit Last Name" do
    it "then it can not be empty"  do      
      text_field(name: "lastName", id: "lastName").clear
      button(text: "Submit").click      
      span(class: "b-form__error", id: "lastName.errors", text: "Please enter info here.").wait_until_present(5)
    end
    it "then it can contain les than or equal to 50 characters"  do      
      text_field(name: "lastName", id: "lastName").clear
      text_field(name: "lastName", id: "lastName").set str(55)
      button(text: "Submit").click       
      span(class: "b-form__error", id: "lastName.errors", text: "Can contain less than or equal to 50 characters").wait_until_present(5)
    end
  end

end

#**************************************************************************************************#
shared_context 'User email' do

  context "when submit Email (Username)" do
    it "then it can not be empty"  do      
      text_field(name: "name", id: "name").clear
      button(text: "Submit").click         
      browser.wait(4)               
      if (select_list(name: "selectedRole", id: "selectedRole").selected? 'Mentee') or 
         (select_list(name: "selectedRole", id: "selectedRole").selected? 'Mentor') then
          span(class: "b-form__error", id: "name.errors", text: "Please enter info here.").wait_until_present(5)
        else
          span(class: "b-form__error", id: "name.errors", text: "Please use company email. Personal emails are not allowed as admin!").wait_until_present(5)
      end
    end

    it "then it should contain less or equal to 255 characters"  do      
      text_field(name: "name", id: "name").clear
      text_field(name: "name", id: "name").set str(260)
      button(text: "Submit").click              
      span(class: "b-form__error", id: "name.errors", text: "Invalid email format").wait_until_present(4)
    end
  end 
end

#**************************************************************************************************#
shared_context 'authorization code' do
  context 'when submit authorization code' do
    it 'then it can not be blanck' do
      text_field(name: "registerCode", id: "registerCode").clear
      button(text: "Submit").click
      browser.wait(3)
      if select_list(name: "selectedRole", id: "selectedRole").selected? 'Employer' then
        span(class: "b-form__error", id: "registerCode.errors", text: "Please enter info here.").wait_until_present(3)  
      else
        span(class: "b-form__error", id: "registerCode.errors", text: "Wrong authorization code").wait_until_present(3)
      end
    end
    
    it 'then it should be less or equal to 20 characters' do
      text_field(name: "registerCode", id: "registerCode").clear
      text_field(name: "registerCode", id: "registerCode").set str(22)
      button(text: "Submit").click             
      if select_list(name: "selectedRole", id: "selectedRole").selected? 'Employer' then
        span(class: "b-form__error", id: "registerCode.errors", text: "Can only have 6-20 characters").wait_until_present(3)
      else
        span(class: "b-form__error", id: "registerCode.errors", text: "Wrong authorization code").wait_until_present(3)
      end
    end
  end
end

#**************************************************************************************************#
shared_context 'Employer registration' do      
  context 'when submit Organization Name' do
    it 'then it can not be empty' do
      text_field(name: "companyName", id: "companyName").clear
      button(text: "Submit").click       
      span(class: "b-form__error", id: "companyName.errors", text: "Please enter info here.").wait_until_present(5)
    end
    it 'then it should contain less or equal to 50 characters' do
      text_field(name: "companyName", id: "companyName").clear
      text_field(name: "companyName", id: "companyName").set str(55)
      button(text: "Submit").click       
      span(class: "b-form__error", id: "companyName.errors", text: "Can contain less than or equal to 50 characters").wait_until_present(5)
    end    
  end
      
  context 'when submit Organization Name' do
    it 'first symbol is not a letter -1' do
    #'then it can contain only letters, digits and "-", "_", "," ," " symbols with a leading letter' do
      validate_orgName('1234')      
    end
    it 'first symbol is not a letter -2' do
      validate_orgName('_a_')
    end
    it 'first symbol is not a letter -3' do
      validate_orgName('-aa-')      
    end
    it 'first symbol is not a letter -4' do
      validate_orgName('    ehukv v')
    end
    it 'first symbol is not a letter -5' do
      validate_orgName(', ABC')
    end
    it 'first symbol is not a letter -6' do
      validate_orgName('   ')
    end
    it 'contain  &' do
      validate_orgName('A&co')
    end
    it 'contain -, begin with number' do
      validate_orgName('123-Lab')
    end
    it 'correct name -1' do
      validate_orgName_true('A-')
    end
    it 'correct name -2' do
      validate_orgName_true('A-1 2345')
    end
    it 'correct name -3' do
      validate_orgName_true('Abc-12 g_3d4f')
    end
    it 'correct name -4' do
      validate_orgName_true('A,')
    end
    it 'correct name -5' do
      validate_orgName_true('A,,,,,,,')
    end
    it 'correct name -6' do
      validate_orgName_true('A_')
    end
    it 'correct name -7' do
      validate_orgName_true('Abc -')
    end
    it 'correct name -8' do
      validate_orgName_true('Abc, C, D')
    end
    it 'correct name -8' do
      validate_orgName_true('Abc ab, 123, 123')
    end    
  end

  context 'when submit fax' do
    it 'then it should be less or equal to 20 characters' do
      text_field(name: "companyFax", id: "companyFax").set "111111111111111111111111"
      button(text: "Submit").click
      browser.wait(3) 
      span(class: "b-form__error",  id: "companyFax.errors", text: 'Should be less or equal to 20 characters').wait_until_present(3)
    end
  end
end

#****************************************************************************************************#
shared_context 'User address' do 


  context 'when submit address' do
    it 'then it can not be empty' do
      browser.refresh
      text_field(name: "postAddressForm.street", id:"postAddressForm.street").clear
      button(class: "b-button b-form__input_submit", value: "Submit").click
      span(id: "postAddressForm.street.errors", text: "Please enter info here.")
      .wait_until_present(5)
    end 
    it 'then it can contain less than or equal to 60 characters' do
      browser.refresh
      text_field(name: "postAddressForm.street", id:"postAddressForm.street").clear
      text_field(name: "postAddressForm.street", id:"postAddressForm.street").set str(65)
      button(class: "b-button b-form__input_submit", value: "Submit").click    
      span(id: "postAddressForm.street.errors", text: 'Should be less or equal to 60 characters')
      .wait_until_present(5)           
    end
  end

  context 'when submit city' do
    it 'then it can not be empty' do
      text_field(name: "postAddressForm.city", id:"postAddressForm.city").clear
      button(class: "b-button b-form__input_submit", value: "Submit").click
      span(id: "postAddressForm.city.errors", text: "Please enter info here.")
      .wait_until_present(5)
    end 
    it 'then it can contain less than or equal to 50 characters' do
      text_field(name: "postAddressForm.city", id: "postAddressForm.city").clear
      text_field(name: "postAddressForm.city", id: "postAddressForm.city").set str(55)
      button(class: "b-button b-form__input_submit", value: "Submit").click    
      span(id: "postAddressForm.city.errors", text: 'Should be less or equal to 50 characters')
      .wait_until_present(5)           
    end
  end
          
  context 'when submit zip' do
    it 'then it can not be empty' do
      text_field(id: "postAddressForm.zip", name: "postAddressForm.zip").wait_until_present(6)
      text_field(id: "postAddressForm.zip", name: "postAddressForm.zip").clear
      button(class: "b-button b-form__input_submit", value: "Submit").click
      span(id: "postAddressForm.zip.errors", text: "Please enter info here.").wait_until_present(5)
    end 
    it 'then it can contain less than or equal to 6 characters' do
      text_field(name: "postAddressForm.zip", id: "postAddressForm.zip").wait_until_present(6)
      text_field(name: "postAddressForm.zip", id: "postAddressForm.zip").clear
      text_field(name: "postAddressForm.zip", id: "postAddressForm.zip").set "1234567"
      button(class: "b-button b-form__input_submit", value: "Submit").click    
      span(id: "postAddressForm.zip.errors", text: 'Should be 6 characters')
      .wait_until_present(5)           
    end
  end
end

#****************************************************************************************************#
shared_context 'address state' do
  context 'when submit state' do
    it 'then it can not be empty' do
      text_field(name: "postAddressForm.state", id:"postAddressForm.state").clear
      button(value: "Submit").click
      span(id: "postAddressForm.state.errors", text: "Please enter info here.").wait_until_present(5)
    end
    it 'then it should contain less or equal to 200 characters' do
      text_field(name: "postAddressForm.state", id: "postAddressForm.state").clear
      text_field(name: "postAddressForm.state", id: "postAddressForm.state").set str(55)
      button(value: "Submit").click    
      span(id: "postAddressForm.state.errors", text: 'Should be less or equal to 50 characters').wait_until_present(5)
    end
  end
end



